package com.sumapay.fsb.common.service.impl;

import com.alibaba.fastjson.JSON;
import com.sumapay.fsb.common.service.SumaService;
import com.sumapay.fsb.gensrc.service.IFsbSmsRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
public class SumaServiceImpl implements SumaService {

    private RestTemplate restTemplate;

    @Value("${rest.http.queryQrCode.url}")
    private String qryUrl;

    @Value("${rest.http.pay.url}")
    private String payUrl;

    @Override
    public String qrCodeQuery(MultiValueMap<String, Object> map) {

        ResponseEntity responseEntity = restTemplate.postForEntity(qryUrl,
                map, String.class, (Object) null);
        System.out.println("-------" + responseEntity.getBody().toString());

        return responseEntity.getBody().toString();
    }


    @Override
    public String qrCodePay(MultiValueMap<String, Object> map) {


        //先校验短信验证码
        if(!iFsbSmsRequestService.checkSms(map)){
            Map<String,Object> returnMap = new HashMap<>();
            returnMap.put("message","验证码输入错误或已超时,请检查短信验证码");
            return JSON.toJSONString(returnMap);
        }


        ResponseEntity responseEntity = restTemplate.postForEntity(payUrl,
                map, String.class, (Object) null);
        System.out.println("-------" + responseEntity.getBody().toString());


        String result = responseEntity.getBody().toString();


        return result;
    }


    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
    private IFsbSmsRequestService iFsbSmsRequestService;

    @Autowired
    public void setiFsbSmsRequestService(IFsbSmsRequestService iFsbSmsRequestService){
        this.iFsbSmsRequestService = iFsbSmsRequestService;
    }
}
